package com.googlecode.jmicroerp.wicket.stocuri;

import com.googlecode.jmicroerp.model.Recipient;
import com.googlecode.jmicroerp.model.Stoc;
import com.googlecode.jmicroerp.model.Aprovizionare;
import com.googlecode.jmicroerp.service.GenericService;
import com.googlecode.jmicroerp.wicket.DefaultEditPage;
import com.googlecode.jmicroerp.wicket.SecuredWebSession;
import com.googlecode.jmicroerp.wicket.index.IndexPage;

import java.math.BigDecimal;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import java.util.Date;


/**
 *
 * @author Alexandru.Codescu
 */
public class EditareStocPage extends DefaultEditPage {
    @SpringBean(name = "genericService")
    private GenericService<Stoc> stocuriService;
    @SpringBean(name = "genericService")
    private GenericService<Recipient> recipientService;
    @SpringBean(name = "genericService")
    private GenericService<Aprovizionare> aprovizionareService;
    
    private Stoc stoc;
    private Aprovizionare aprovizionare;
    private BigDecimal cantitateOriginala;
    
    public EditareStocPage(Long idStoc) {
        stoc = stocuriService.getById(Stoc.class, idStoc);
        cantitateOriginala = stoc.getCantitate();
        aprovizionare = new Aprovizionare();
        initPage();
    }
    
    private void initPage() {
        Form<Stoc> form = new Form<Stoc>("form");
        form.setModel(new CompoundPropertyModel<Stoc>(stoc));
        
        final Label lblMagazin = new Label("magazin", stoc.getRecipient().getMagazin().getDenumire());
        form.add(lblMagazin);
        final Label lblProdus = new Label("produs", stoc.getProdus().getDenumire());
        form.add(lblProdus);
        final Label ddcRecipient = new Label("recipient", stoc.getRecipient().getDenumire());
        form.add(ddcRecipient);
        
        final Label lblCantOld = new Label("cantitateOld", stoc.getCantitate());
        form.add(lblCantOld);
        
        TextField<BigDecimal> tfCantitate = new TextField<BigDecimal>("cantitate");
        tfCantitate.setRequired(true);
        form.add(tfCantitate);
        
        SubmitLink salvare = new SubmitLink("submit") {

            @Override
            public void onSubmit() {
                stoc.setCantitate(cantitateOriginala.add(stoc.getCantitate()));
                stocuriService.update(stoc);
                
                aprovizionare.setRecipient(stoc.getRecipient());
                aprovizionare.setCantitate(stoc.getCantitate().subtract(cantitateOriginala));   
                aprovizionare.setdataModificarii(new Date());
                aprovizionare.setUtilizator(((SecuredWebSession) getSession()).getUserDetail().getUtilizator());
                aprovizionareService.insert(aprovizionare);
                
                setResponsePage(IndexPage.class);
            }
            
        };
        form.add(salvare);
        
        form.add(new Link("renuntare") {

            @Override
            public void onClick() {
                setResponsePage(IndexPage.class);
            }
            
        });
        
        add(form);
    }
}
